//
// Created by francklinson on 2021/10/23 AT 15:43.
//
#include <iostream>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <algorithm>
#include <functional>
#include <climits>
#include <string>
#include "../myTool.h"
#include <chrono>
#include <array>

//using namespace std;
class Timer {
public:
    Timer() {
        m_StartTimePoint = std::chrono::high_resolution_clock::now();
    }

    ~Timer() {
        Stop();
    }

    void Stop() {
        auto endTimePoint = std::chrono::high_resolution_clock::now();
        auto start = std::chrono::time_point_cast<std::chrono::microseconds>(
                m_StartTimePoint).time_since_epoch().count();
        auto end = std::chrono::time_point_cast<std::chrono::microseconds>(endTimePoint).time_since_epoch().count();
        auto duration = end - start;
        double ms = duration * 0.001;
        std::cout << duration << "us (" << ms << "ms)\n";
    }

private:
    std::chrono::time_point<std::chrono::high_resolution_clock> m_StartTimePoint;

};

int main() {
    int value = 0;
    {
        Timer timer;
        // 编译器会优化
        for (int i = 0; i < 100000; ++i) {
            value += 2;
        }
    }
    struct Vector2 {
        float x, y;
    };
    std::cout << "make shared:" << std::endl;
    {
        std::array<std::shared_ptr<Vector2>, 100> sharedPtrs;
        Timer timer;
        for (int i = 0; i < sharedPtrs.size(); ++i) {
            sharedPtrs[i] = std::make_shared<Vector2>();

        }
    }
    std::cout << "new shared:" << std::endl;
    {
        std::array<std::shared_ptr<Vector2>, 100> sharedPtrs;
        Timer timer;
        for (int i = 0; i < sharedPtrs.size(); ++i) {
            sharedPtrs[i] = std::shared_ptr<Vector2>(new Vector2());

        }
    }
    std::cout << "make unique:" << std::endl;
    {
        std::array<std::unique_ptr<Vector2>, 100> sharedPtrs;
        Timer timer;
        for (int i = 0; i < sharedPtrs.size(); ++i) {
            sharedPtrs[i] = std::make_unique<Vector2>();

        }
    }
    /**耗时：
     * unique < make_shared< new shared
     */
    std::cout << value << std::endl;

}

